package com.yang.zngcxyxkjs.service.impl;

import com.yang.zngcxyxkjs.api.FaceSearch;
import com.yang.zngcxyxkjs.mapper.AdminMapper;
import com.yang.zngcxyxkjs.mapper.TeacherMapper;
import com.yang.zngcxyxkjs.pojo.Admin;
import com.yang.zngcxyxkjs.pojo.Teacher;
import com.yang.zngcxyxkjs.service.AdminService;
import com.yang.zngcxyxkjs.service.TeacherService;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired(required = false)
    private AdminService adminService;

    @Autowired
    FaceSearch faceSearch;

    @Override
    public Admin adminCompareFace(String imgUrl) throws Exception{
        Admin admin = null;
        //调用方法查看人脸搜索结果
        JSONObject res = faceSearch.searchbase64(imgUrl, "group1");
        //获取分数/人脸相似度，可以通过查看返回结果看如何获取
        Object  score = res.getJSONObject("result")
                .getJSONArray("user_list")
                .getJSONObject(0).get("score");
        //获取user_id
        Object  faceid = res.getJSONObject("result")
                .getJSONArray("user_list")
                .getJSONObject(0).get("user_id");
        double sc = Double.parseDouble(score.toString());
        //人脸相似度大于80才过
        System.out.println("人脸分数:"+score);
        System.out.println("用户id"+faceid);
        if (sc > 80) {
            //查询数据库是否有一样的userid,数据库字段是faceid

            List<Admin> admins = adminMapper.selectAdminByFaceId(faceid);
            if (admins == null || admins.size() == 0) {
                return null;
            } else {
                return admins.get(0);
            }
        }
        return null;
    }

}
